package com.fz.us.admin.company.service;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PagePaging;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.company.dao.CompanyDao;
import com.fz.us.admin.company.entity.*;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Service接口 - 组织
 * ============================================================================
 * 版权所有 2017 。
 *
 * @author fallenpanda
 * @version 1.0 2017-03-26 。
 * ============================================================================
 */
@Service
public class OrganizeService {

    @Resource
    private CompanyService companyService;
    @Resource
    private UserService userService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private PostService postService;
    @Resource
    private PowerService powerService;
    @Resource
    private DutyService dutyService;

    /**
     * 获取职责
     *
     * @param powerId
     * @return
     */
    public Power getPower(String powerId) {
        return powerService.get(powerId);
    }



    /**
     * 获取职权
     *
     * @param dutyId
     * @return
     */
    public Duty getDuty(String dutyId) {
        return dutyService.get(dutyId);
    }

    /**
     * 用户职权
     *
     * @param user
     * @return
     */
    public List<Duty> getUserDutyList(User user) {
        List<Duty> dutyList = dutyService.getUserDutyList(user);
        return dutyList==null?new ArrayList<Duty>():dutyList;
    }

    /**
     * 用户默认职权
     *
     * @param user
     * @return
     */
    public Duty getUserDefaultDuty(User user) {
        return dutyService.getUserDefaultDuty(user);
    }

    /**
     * 职权下用户
     *
     * @param power
     * @return
     */
    public List<Duty> getDutyListByPower(Power power) {
        List<Duty> dutyList = dutyService.getDutyListByPower(power);
        return dutyList==null?new ArrayList<Duty>():dutyList;
    }

    /**
     * 部门下用户
     *
     * @param department
     * @return
     */
    public List<Duty> getDutyListByDepartment(Department department) {
        List<Duty> dutyList = dutyService.getDutyListByDepartment(department);
        return dutyList==null?new ArrayList<Duty>():dutyList;
    }



    /**
     * 岗位下用户
     *
     * @param post
     * @return
     */
    public List<Duty> getDutyListByPost(Post post) {
        List<Duty> dutyList = dutyService.getDutyListByPost(post);
        return dutyList==null?new ArrayList<Duty>():dutyList;
    }

    /**
     * 用户上级职权
     *
     * @param duty
     * @return
     */
    public List<Duty> getSuperDutyList(Duty duty) {
        if (duty != null) {
            if (duty.getPower().getParent() != null) {
                Power parentPower = duty.getPower().getParent();
                return getDutyListByPower(parentPower);
            }
        }
        return new ArrayList<Duty>();
    }


    /**
     * 获取职责(要舍弃)
     *
     * @param company
     * @param value
     * @return
     */
    public Power getPowerByValue(Company company, String value) {
        return powerService.getPowerByValue(company, value);
    }

    /**
     * 分页 用户列表
     *
     * @param page
     * @param rules
     * @param order
     * @param company
     * @return
     */
    public SearchResultPaging pagingDuty(PagePaging page, List<Rule> rules, Order order, Company company) {
        return dutyService.pagingDuty(page, rules, order, company, BaseEnumManager.StateEnum.Enable);
    }

}
